package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.Admin;
import pojo.Teacher;
import service.AdminService;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author tao
 * @date 2021/12/11
 */
@Controller
@RequestMapping("/hms")
public class AdminController {

    @Autowired
    @Qualifier("adminServiceImpl")
    private AdminService adminService;

    @RequestMapping("/adminLogin")
    public String login(){
        return "adminLogin";
    }

    @RequestMapping("/adminLogin1")
    public String login1(Admin admin, Model model, HttpSession session){
        System.out.println("aadadada");
        if(adminService.selectId(admin.getId())>0){
            if (adminService.selectPassword(admin.getId()).equals(admin.getPassword())){
                if (session.getAttribute("admin")!=null){
                    return "redirect:/hms/admin";
                }
                session.setAttribute("admin",admin);
                return "redirect:/hms/admin";
            } else {
                model.addAttribute("Register", "用户名或密码错误");
                return "redirect:/hms/adminLogin";
            }
        } else {
            model.addAttribute("Register", "用户名或密码错误");
            return "redirect:/hms/adminLogin";
        }
    }

    @RequestMapping("/admin")
    public String index1(Model model, HttpSession session){
        List<Teacher> teachers = adminService.selectTeacher();
        model.addAttribute("teacher",teachers);
        return "admin/admin";
    }

    @ResponseBody
    @RequestMapping("/addTeacher")
    public String addTeacher(Teacher teacher){
        System.out.println(teacher);
        adminService.addTeacher(teacher);
        return "";
    }



}
